package com.netease.nie.base.utils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

/**
 * 
 * @author KingsHunter
 * @createDate August 17,2016
 *
 */
public class CustomWebUtils {
	
	private static final String DOMAIN = ".163.com";
	
	/**
     * 获取客户端ip地址
     * 
     * @return
     */
    public static String getIp(HttpServletRequest request) {
    	// 优先从x-forwarded-for获取
    	String ip = request.getHeader("x-forwarded-for");
        if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip))
        	ip = request.getHeader("Proxy-Client-IP");
        if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip))
        	ip = request.getHeader("WL-Proxy-Client-IP");
        if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip))
        	ip = request.getRemoteAddr();
        
        if (StringUtils.isNotBlank(ip) && !"unknown".equalsIgnoreCase(ip))
        	return ip.split(",")[0];
        return null;
    }
    
    public static String getCookieValue(HttpServletRequest request, String key) {
		String value = null;
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if (key.equals(cookie.getName())) {
					if (StringUtils.isNotBlank(cookie.getValue())) {
						value = cookie.getValue();
						break;
					}
				}
			}
		}
		return value;
	}
    
    public static boolean existsCookie(HttpServletRequest request, String key) {
    	boolean exists = false;
    	Cookie[] cookies = request.getCookies();
    	if (cookies != null) {
    		for (Cookie cookie : cookies) {
				if (key.equals(cookie.getName())) {
					exists = true;
					break;
				}
			}
    	}
    	return exists;
    }
    
    public static void addCookieValue(HttpServletResponse response, String key, String value) {
    	Cookie cookie = new Cookie(key, value);
    	cookie.setMaxAge(-1);
    	cookie.setDomain(DOMAIN);
    	cookie.setPath("/");
    	response.addCookie(cookie);
    }
    
    public static void deleteCookieValue(HttpServletRequest request, HttpServletResponse response, String key) {
    	boolean exists = existsCookie(request, key);
    	if (exists) {
    		Cookie cookie = new Cookie(key, null);
        	cookie.setMaxAge(0);
        	cookie.setDomain(DOMAIN);
        	cookie.setPath("/");
        	response.addCookie(cookie);
    	}
    }
    
    /**
     * 获取user-agent
     * 
     * @return
     */
    public static String getUserAgent(HttpServletRequest request) {
    	String ua = request.getHeader("user-agent");
        if (StringUtils.isNotBlank(ua))
        	return ua;
        return StringUtils.defaultIfEmpty(ua, StringUtils.EMPTY);
    }
    
    /**
     * 来源页面
     * 
     * @return
     */
    public static String getRequesReferUrl(HttpServletRequest request) {
        String referer = request.getHeader("referer");
        if (StringUtils.isNotBlank(referer))
        	return referer;
        return StringUtils.defaultIfEmpty(referer, StringUtils.EMPTY);
    }
    
}
